import React from 'react'
import PropTypes from 'prop-types'
import { Switch, Route, Redirect, routerRedux } from 'dva/router'
import dynamic from 'dva/dynamic'
import HomePage from './routes/HomePage'

const { ConnectedRouter } = routerRedux

const Routers = function ({ history, app }){
    const routes = [{
            path:'/login',
            models:()=>[import('./models/login')],
            component:()=>import('./routes/Login')
        },{
            path: '/dashboard',
            component: () => import('./routes/IndexPage')
        },{
            path: '/user/userlist',
            models: () => [import('./models/user')],
            component: () => import('./routes/user/UserList')
        },{
            path:'/menu/menulist',
            models:()=>[import('./models/menu')],
            component:()=>import('./routes/menu/MenuList')
        },{
            path:'/permission/usergrouplist',
            models:()=>[import('./models/usergroup')],
            component:()=>import('./routes/permission/UserGroupList')
        }
    ]
    return (
        <ConnectedRouter history={history}>
            <HomePage>
                <Switch>
                    <Route exact path="/" render={() => (<Redirect to="/dashboard" />)}/>
                    {
                        routes.map(({ path, ...dynamics }, key) => (
                            <Route key={key}
                                   exact
                                   path={path}
                                   component={dynamic({
                                       app,
                                       ...dynamics,
                                   })}
                            />
                        ))
                    }
                </Switch>
            </HomePage>
        </ConnectedRouter>
    )
}

Routers.propTypes = {
    history: PropTypes.object,
    app: PropTypes.object,
}

export default Routers;
